﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.PO;
using Com.CMS263.BLL;
using Com.CMS263.Util;

namespace Com.CMS263.Web.manage
{
    public partial class ArticleList : System.Web.UI.Page
    {
        public string typeName;
        private static int pageSize = 30;
        ArticleBLL abll = new ArticleBLL();
        ArtTypeBLL atbll = new ArtTypeBLL();
       public  int page = 1;
       public int pid;
        protected void Page_Load(object sender, EventArgs e)
        {
            pid = int.Parse(Request["pid"]);
            page=string.IsNullOrEmpty(Request["page"]) ? 1 : int.Parse(Request["page"]);
            Com.CMS263.PO.ArtType type = atbll.GetArtTypeById(pid);
            this.lbTypeName.Text = type.typeName;
            this.key.Text = string.IsNullOrEmpty(Request["key"]) ? "" : Request["key"];
            this.BindList(pid,page,this.key.Text);
        }

        public void BindList(int pid,int page,string key)
        {
            IList<Article> list = null;
            if (string.IsNullOrEmpty(this.key.Text)) 
            {
                list = abll.GetArticleById(pid, page, pageSize);
            }
            else
            {
                 list = abll.GetArticleById(pid, page, pageSize, key);
            }
            this.artList.DataSource = list;
            this.DataBind();
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
           
        }
        public string GetPager()
        {
            int count = 0;
            if (string.IsNullOrEmpty(this.key.Text))
            {
                count = abll.GetTotalByPid(pid);
            }
            else
            {
                count= abll.GetTotalByPid(pid, this.key.Text);
            }
          return  Pager.GetPager(count, pageSize, page, "&pid="+pid+"&key="+this.key.Text);
      
        }

        protected void artList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = int.Parse(e.CommandArgument.ToString());
            abll.DeleteArtById(id);
            int pid=int.Parse(Request["pid"]);
            int page=string.IsNullOrEmpty(Request["page"])?1:int.Parse(Request["page"]);
            string key=StringUtil.checkStr(Request["key"]);
            this.BindList(pid, page, key);
        }
    }
}
